import numpy as np
import matplotlib.pyplot as plt
from scipy.fftpack import idct

def inverse_dct_and_plot(compressed_data, original_size=500):
    """
    Perform inverse DCT on compressed data, normalize the reconstructed signal, and plot it.

    Parameters:
    compressed_data (list or np.ndarray): The compressed data (e.g., 150 samples).
    original_size (int): The size of the original signal (default is 500 samples).
    """
    # Pad the compressed data with zeros to match the original size
    padded_data = np.zeros(original_size)
    padded_data[:len(compressed_data)] = compressed_data

    # Perform the inverse DCT
    reconstructed_signal = idct(padded_data, norm='ortho')

    # Normalize the reconstructed signal
    reconstructed_signal = (reconstructed_signal - np.min(reconstructed_signal)) / (np.max(reconstructed_signal) - np.min(reconstructed_signal))

    # Plot the reconstructed signal
    plt.figure(figsize=(10, 5))
    plt.plot(reconstructed_signal, label="Reconstructed Signal (Normalized)")
    plt.title("Reconstructed Signal from Compressed Data")
    plt.xlabel("Sample Index")
    plt.ylabel("Normalized Amplitude")
    plt.legend()
    plt.grid()
    plt.show()

# Example usage:
compressed_data = [
        24003.97,
        -461.52,
        243.92,
        -721.06,
        511.64,
        -49.97,
        352.68,
        -615.21,
        61.85,
        -112.34,
        465.45,
        -938.39,
        26.16,
        98.45,
        545.42,
        -800.33,
        89.58,
        -42.22,
        791.5,
        -212.69,
        -158.48,
        -302.84,
        758.78,
        -44.41,
        -138.95,
        -458.27,
        491.38,
        100.24,
        45.08,
        -603.55,
        127.31,
        265.7,
        208.69,
        -579.88,
        -167.2,
        186.71,
        313.21,
        -188.41,
        -348.83,
        173.71,
        284.51,
        206.2,
        -410.89,
        -87.55,
        124.78,
        416.59,
        -249.41,
        -56.09,
        -121.02,
        503.23,
        -195.05,
        -36.67,
        -401.83,
        346.64,
        15.62,
        200.04,
        -376.26,
        209.56,
        2.37,
        269.82,
        -300.04,
        109.77,
        -35.83,
        265.07,
        -285.53,
        -49.11,
        -160.26,
        340.87,
        -128,
        174.32,
        -241,
        173.53,
        56.43,
        69.46,
        -123.94,
        136.49,
        -50.51,
        152.92,
        -187.56,
        -70.95,
        -34.53,
        161.39,
        38.93,
        -112.44,
        -14.09,
        67.53,
        -8.31,
        2.53,
        -43.45,
        59.36,
        88.79,
        -1.17,
        -0.17,
        60.55,
        2.98,
        -72.91,
        -74.62,
        -27.28,
        -24.53,
        -11.88,
        -78.51,
        -47.6,
        24.1,
        38.39,
        -23.64,
        39.59,
        -9.33,
        -32.89,
        -94.01,
        65.74,
        8.81,
        177.61,
        -68.16,
        65.35,
        -52.07,
        41.62,
        -133.73,
        -69.17,
        -75.67,
        -66.88,
        -30.64,
        58.42,
        -273.06,
        -278.05,
        84.88,
        -5.73,
        -16.57,
        -37.31,
        -7.37,
        80.39,
        -27.98,
        -93.91,
        -45.7,
        110.01,
        -50.22,
        2.17,
        90.04,
        6.49,
        -28.7,
        -80.88,
        -8.95,
        26.68,
        -91.02,
        1.11,
        -74.94,
        8.38,
        110.11,
        -1.98,
        -17.89,
        147.08,
        41.69,
        -41.84,
        -81.53,
        -61.36,
        21.72,
        -100.43,
        56.49,
        -29.14,
        -6.61,
        -76.81,
        91.76,
        42.06,
        -30.66,
        1.47,
        175.83,
        -89.37,
        85.67,
        -78.98,
        22.82,
        -31.39,
        82.31,
        -123.5,
        26.5,
        28.1,
        149.81,
        -4.39,
        -61.54,
        -11.65,
        51.37,
        11.93,
        -16.72,
        -61.36,
        -6.91,
        -8.3,
        111.2,
        -1.46,
        34.62,
        86.52,
        4.74,
        -104.09,
        -17.66,
        -69.05,
        -36.72,
        -99.1,
        43.28,
        -38.44,
        95.58,
        -48.17,
        53.41,
        12.23,
        18.89,
        96.05,
        170.35,
        -37.1,
        55.19,
        -33.62,
        -6.94,
        -89.2,
        -62.67,
        -34.48,
        38.08,
        -7.94,
        -85.85,
        54.75,
        4.88,
        -2.71,
        -5.41,
        55.37,
        -46.21,
        3.6,
        12.18,
        -15.74,
        24.16,
        -23.65,
        16.35,
        46.05,
        -36.95,
        -70.21,
        -69.74,
        -85.4,
        -39.66,
        38.26,
        0.53,
        -38.7,
        -73.59,
        11.98,
        46.3,
        39.36,
        7,
        -10.37,
        -20.86,
        24.58,
        -18.93,
        -31.35,
        -12,
        51.03,
        -2.25,
        1.94,
        44,
        25.6]  # Replace with actual compressed data (150 samples)
inverse_dct_and_plot(compressed_data)
